/**
 * 
 */
package com.teamall.web.family.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.teamall.web.app.db.dao.AppCommonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teamall.core.common.Page;
import com.teamall.core.convert.IArgConversionService;
import com.teamall.core.idproduce.ISequenceGenerator;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.Utils;
import com.teamall.web.family.db.arg.FamilyHelpArg;
import com.teamall.web.family.db.dao.FamilyHelpDao;
import com.teamall.web.family.db.po.FamilyHelpPO;
import com.teamall.web.family.service.IFamilyHelpService;

/**
 * <Description> <br>
 * 
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.family.service.impl <br>
 */

@Service("familyHelpService")
public class FamilyHelpServiceImpl implements IFamilyHelpService {

    private static final LogManager logger = LogManager
            .getLogger(FamilyHelpServiceImpl.class);

    @Autowired
    private FamilyHelpDao familyHelpDao;
    @Autowired
    private AppCommonDao appCommonDao;

    /**
     * 查询条件转换成Arg类的服务接口
     */
    @Resource(name = "defaultArgConversionService")
    private IArgConversionService argConversionService;

    /**
     * 主键生成器
     */
    @Resource(name = "sequenceProcGenerator")
    private ISequenceGenerator sequenceGenerator;
    

    @Override
    public FamilyHelpPO selectByPrimaryKey(Integer key) throws BaseAppException {
        // ///////
        // TODO 根据业务场景，设置查询条件、数据校验等

        // ///////
        return familyHelpDao.selectByPrimaryKey(key);
    }

    @Override
    public List<FamilyHelpPO> selectByArg(FamilyHelpPO record) throws BaseAppException {
        logger.debug("selectByArg begin...record={0}", record);
        record.setOrderField("HELP_DATE");
        record.setOrderDirection("DESC");
        FamilyHelpArg arg = new FamilyHelpArg();
        FamilyHelpArg.FamilyHelpCriteria criteria = arg.createCriteria();
        if(Utils.notEmpty(record.getHelpStaff())){
            criteria.andHelpStaffEqualTo(record.getHelpStaff());
        }
        if (Utils.notEmpty(record.getFeedbackStaff())){
            criteria.andFeedbackStaffEqualTo(record.getFeedbackStaff());
        }
        if (Utils.notEmpty(record.getHelpState())){
            criteria.andHelpStateEqualTo(record.getHelpState());
        }
        if (Utils.notEmpty(record.getTownId())){
            criteria.andTownIdEqualTo(record.getTownId());
        }
        if  (Utils.notEmpty(record.getVillageId())){
            criteria.andVillageIdEqualTo(record.getVillageId());
        }
        if(Utils.notEmpty(record.getHelpContent())){
            criteria.andHelpContentLike(record.getHelpContent());
        }
        arg.setOrderByClause("help_date desc");
        return familyHelpDao.selectByArg(arg);
    }

    @Override
    public Page<FamilyHelpPO> selectByArgAndPage(FamilyHelpPO record, Page<FamilyHelpPO> resultPage)
            throws BaseAppException {
        logger.debug("selectByArgAndPage begin...record={0}", record);
        FamilyHelpArg arg = new FamilyHelpArg();
        FamilyHelpArg.FamilyHelpCriteria criteria = arg.createCriteria();
        if(Utils.notEmpty(record.getHelpStaff())){
            criteria.andHelpStaffEqualTo(record.getHelpStaff());
        }
        if (Utils.notEmpty(record.getFeedbackStaff())){
            criteria.andFeedbackStaffEqualTo(record.getFeedbackStaff());
        }
        if (Utils.notEmpty(record.getHelpState())){
            criteria.andHelpStateEqualTo(record.getHelpState());
        }
        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        // FamilyHelpArg arg = new FamilpArg();
        // //TODO 根据业务场景，设置查询条lyHe件，示例
        // FamilyHelpCriteria criteria = arg.createCriteria();
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }

        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        arg.setOrderByClause("help_date desc");
        resultPage = familyHelpDao.selectByArgAndPage(arg, resultPage);


        return resultPage;
    }

    @Override
    public int add(FamilyHelpPO record) throws BaseAppException {
        logger.debug("add begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
        // int pkId = sequenceGenerator.sequenceIntValue("表名","主键名");
        // record.setUserId(pkId);
        // record.setCreatedDate(new Date());
        // ///////

        return familyHelpDao.insertSelective(record);
    }

    @Override
    public int update(FamilyHelpPO record) throws BaseAppException {
        logger.debug("update begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

        // ///////

        return familyHelpDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(FamilyHelpPO record) throws BaseAppException {
        logger.debug("delete begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行关联性校验、关联删除等

        // ///////

        return familyHelpDao.deleteByPrimaryKey(record.getHelpId());
    }

    @Override
    public Integer selectByUser(Integer userObjType,Integer userObjId,FamilyHelpPO po,Page<FamilyHelpPO> resultPage) throws BaseAppException {
        Integer notSolveHelpCount = 0;
        if(userObjType == 0){
            po.setFeedbackStaff(userObjId);
            notSolveHelpCount = appCommonDao.getNotSolveHelpByFeedBackId(userObjId);
        }else{
            po.setHelpStaff(userObjId);
            notSolveHelpCount = appCommonDao.getNotSolveHelpByHelpId(userObjId);
        }
        this.selectByArgAndPage(po,resultPage);
        return notSolveHelpCount;
    }

}
